﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class Sistema_CAPSesionesInicio
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        hdIdPersona.Value = Request.QueryString.Item("idPersona")
        cargargrid()
        traerPersona()
    End Sub
    Sub traerPersona() 'carga los controles con los datos de la bd
        Using bd As New BdHogaresDataContext
            Dim a = From i In bd.Web_CapPersonas
                      Where i.idPersona = hdIdPersona.Value
                      Select i
            Dim Persona As New Web_CapPersona
            Persona = a.FirstOrDefault()
            txthistoriaclinica.Text = IIf(IsDBNull(Persona.txthistoriaclinica), "", Persona.txthistoriaclinica)
            txtfechaatencion.Text = IIf(IsDBNull(Persona.txtfechaatencion), "", Persona.txtfechaatencion)
            txtapellidos.Text = IIf(IsDBNull(Persona.txtapellidos), "", Persona.txtapellidos)
            txtNombres.Text = IIf(IsDBNull(Persona.txtNombres), "", Persona.txtNombres)
            rbTipodedocumento.Text = LlenarItems(Persona.rbTipodedocumento, "tipodocumento")
            txtNumerodedocumento.Text = IIf(IsDBNull(Persona.txtNumerodedocumento), "", Persona.txtNumerodedocumento)


        End Using
    End Sub

    Function LlenarItems(ByVal valor As Integer, ByVal pregunta As String) As String
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()
            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_OpcionesListarConValor", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@categoria", pregunta)
                cmd.Parameters.AddWithValue("@valoropcion", valor)
                Using drDatos As SqlDataReader = cmd.ExecuteReader
                    While drDatos.Read()
                        Dim a = drDatos.Item(0)
                        Return a
                        cnn.Close()
                    End While
                End Using

            End Using
        End Using
    End Function
    Protected Sub btnNuenva_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNuenva.Click
        Response.Redirect("Capsesiones.aspx?sesionnumero=0&idpersona=" & hdIdPersona.Value)
    End Sub
    Sub cargargrid()
        Using cnn As New SqlConnection(ConfigurationManager.ConnectionStrings("CNNdb").ConnectionString)
            cnn.Open()
            Using cmd As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand("usp_sesionestraergrid", cnn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.AddWithValue("@idpersona", hdIdPersona.Value)
                grdResultados.DataSource = cmd.ExecuteReader()
                grdResultados.DataBind()
            End Using
            cnn.Close()
        End Using


    End Sub
End Class
